package endpoint

import (
	"context"

	"rpcdemo/pb"
	"rpcdemo/service"

	"github.com/go-kit/kit/endpoint"
)

type SumRequest struct {
	A int64 `json:"a"`
	B int64 `json:"b"`
}

type SumResponse struct {
	Result int64  `json:"result"`
	Err    string `json:"err,omitempty"`
}

type ConcatRequest struct {
	A string `json:"a"`
	B string `json:"b"`
}

type ConcatResponse struct {
	Result string `json:"result"`
	Err    string `json:"err,omitempty"`
}

func MakeSumEndpoint(svc service.Service) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		req := request.(SumRequest)
		result, err := svc.Sum(ctx, req.A, req.B)
		if err != nil {
			return SumResponse{result, err.Error()}, nil
		}

		return SumResponse{Result: result}, nil
	}
}

func MakeConcatEndpoint(svc service.Service) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		req := request.(*pb.ConcatRequest)
		result, err := svc.Concat(ctx, req.A, req.B)
		if err != nil {
			return ConcatResponse{result, err.Error()}, nil
		}

		return ConcatResponse{Result: result}, nil
	}
}
